package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.SalPersonType;
import com.boss.salary.entity.SalPost;
import com.boss.salary.service.ISalPostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/salPostController", produces = MediaType.APPLICATION_JSON_VALUE)
public class SalPostController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalPersonTypeController.class);

    @Autowired
    private ISalPostService iSalPostService;

    /**
     * 查询分页数据
     *
     * @return
     */
    @RequestMapping("/querySalPostPage")
    public ApiResultDTO querySalPostPage(@RequestParam(value = "page") String page,
                                         @RequestParam(value = "limit") String limit) {
        try {
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<SalPost> pageDTO = iSalPostService.querySalPostPage(paginationDTO);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 根据code查询
     * @param code
     * @return
     */
    @GetMapping("/queryDataByCode")
    public ApiResultDTO queryDataByCode(@RequestParam("code") String code){
        try {
            int count = iSalPostService.queryDataByCode(code);
            return ApiResultDTO.success(count, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 新增
     *
     * @param salPost
     * @return
     */
    @PostMapping("/saveSalPost")
    public ApiResultDTO saveSalPost(@RequestBody SalPost salPost) {
        try {
            iSalPostService.insertSalPost(salPost);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 人员职务发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 人员职务发生异常");
        }
    }

    /**
     * 修改
     *
     * @param salPost
     * @return
     */
    @PostMapping("/updateSalPost")
    public ApiResultDTO updateSalPost(@RequestBody SalPost salPost) {
        try {
            iSalPostService.updateSalPost(salPost);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 人员职务表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 人员职务发生异常");
        }
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSalPost/{id}")
    public ApiResultDTO deleteSalPost(@PathVariable("id") String id) {
        try {
            iSalPostService.deleteSalPost(id);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 人员职务发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 人员职务发生异常");
        }
    }

    /**
     * 查询全部
     *
     * @return
     */
    @RequestMapping("/querySalPostAll")
    public ApiResultDTO querySalPostAll(@RequestParam(value = "id", required = false) String id) {
        try {
            Map<String, Object> queryParam = new HashMap<>();
            queryParam.put("id", id);
            List<SalPost> salPersonTypeList = iSalPostService.querySalPostAll(queryParam);
            return ApiResultDTO.success(salPersonTypeList, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 关联表分页数据
     *
     * @return
     */
    @RequestMapping("/querySalPersonPostRefPage")
    public ApiResultDTO querySalPersonPostRefPage(@RequestParam(value = "page") String page,
                                                  @RequestParam(value = "limit") String limit) {
        try {
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<Map<String, Object>> pageDTO = iSalPostService.querySalPersonPostRefPage(paginationDTO);
            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 新增人员职务中间表
     *
     * @param salPersonType
     * @return
     */
    @PostMapping("/saveSalPersonPostRef")
    public ApiResultDTO saveSalPersonPostRef(@RequestBody SalPersonType salPersonType) {
        try {
            iSalPostService.saveSalPersonPostRef(salPersonType);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 人员职务发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 人员职务中间表发生异常");
        }
    }

    /**
     * 查询全部
     *
     * @return
     */
    @RequestMapping("/querySalPostByPersonTypeId")
    public ApiResultDTO querySalPostByPersonTypeId(@RequestParam(value = "personTypeId", required = false) String personTypeId) {
        try {
            List<SalPost> salPersonTypeList = iSalPostService.querySalPostByPersonTypeId(personTypeId);
            return ApiResultDTO.success(salPersonTypeList, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

}
